package com.a16.yuejian.yuejian.model;

import com.a16.yuejian.yuejian.javabean.Collected;
import com.a16.yuejian.yuejian.javabean.Img;
import com.a16.yuejian.yuejian.javabean.Success;
import com.a16.yuejian.yuejian.javabean.Train;
import com.a16.yuejian.yuejian.listener.CollectedListener;
import com.a16.yuejian.yuejian.listener.Listener;
import com.a16.yuejian.yuejian.listener.SuccessListener;
import com.a16.yuejian.yuejian.listener.TrainListener;
import com.a16.yuejian.yuejian.service.TrainService;
import com.a16.yuejian.yuejian.service.UserService;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by 55080 on 2017/10/19.
 */

public class TrainModel {
    private Retrofit retrofit;
    public TrainModel(){
        retrofit =new Retrofit.Builder()
                .baseUrl("http://10.0.2.2:8080/neu_yuejian201/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }

    public void alltrain(String grade, final Listener listener){
        TrainService trainService = retrofit.create(TrainService.class);
        Call call = trainService.alltrain(grade);
        Callback<List<Train>> callback = new Callback<List<Train>>() {
            @Override
            public void onResponse(Call<List<Train>> call, Response<List<Train>> response) {
                if (response.isSuccessful() && response.body()!=null)
                {
                    listener.onResponse(response.body());
                }
            }

            @Override
            public void onFailure(Call<List<Train>> call, Throwable t) {

            }
        };
        call.enqueue(callback);
    }

    public void gettrain(String tr_id, final TrainListener listener){
        TrainService trainService = retrofit.create(TrainService.class);
        Call call = trainService.gettrain(tr_id);
        Callback<Train> callback = new Callback<Train>() {
            @Override
            public void onResponse(Call<Train> call, Response<Train> response) {
                if (response.isSuccessful() && response.body()!=null)
                {
                    listener.onResponse(response.body());
                }
            }

            @Override
            public void onFailure(Call<Train> call, Throwable t) {

            }
        };
        call.enqueue(callback);
    }

    public void follow(String tr_id, String uid, String type, final SuccessListener listener){
        TrainService trainService = retrofit.create(TrainService.class);
        Call call = trainService.follow(tr_id,uid,type);
        Callback<Success> callback = new Callback<Success>() {
            @Override
            public void onResponse(Call<Success> call, Response<Success> response) {
                if (response.isSuccessful() && response.body()!=null)
                {
                    listener.onResponse(response.body());
                }
            }

            @Override
            public void onFailure(Call<Success> call, Throwable t) {

            }
        };
        call.enqueue(callback);
    }

    public void isCollected(String tr_id, String uid, String type, final CollectedListener listener){
        TrainService trainService = retrofit.create(TrainService.class);
        Call call = trainService.isCollected(tr_id,uid,type);
        Callback<Collected> callback = new Callback<Collected>() {
            @Override
            public void onResponse(Call<Collected> call, Response<Collected> response) {
                if (response.isSuccessful() && response.body()!=null)
                {
                    listener.onResponse(response.body());
                }
            }

            @Override
            public void onFailure(Call<Collected> call, Throwable t) {

            }
        };
        call.enqueue(callback);
    }

    public void lunbo(final Listener listener){
        TrainService trainService = retrofit.create(TrainService.class);
        Call call = trainService.lunbo();
        Callback<List<Img>> callback=new Callback<List<Img>>() {
            @Override
            public void onResponse(Call<List<Img>> call, Response<List<Img>> response) {
                if (response.isSuccessful() && response.body()!=null)
                {
                    listener.onResponse(response.body());
                }
            }

            @Override
            public void onFailure(Call<List<Img>> call, Throwable t) {

            }
        };
        call.enqueue(callback);
    }
}
